﻿namespace TouchUmlUI
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    
    public partial class Configuration : Window
    {
        public static TouchUMLConfiguration conf;

        public Configuration()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            presetComboBox.SelectedIndex = (int)conf.ApplicationTheme;

            if (conf.background.backgroundType == TouchUMLConfiguration.Background.BackgroundType.SOLID)
            {
                solidRadioButton.IsChecked = true;                
                gradientRadioButton.IsChecked = false;                                
                label1.IsEnabled = false;
                gradientPreview.IsEnabled = false;
            }
            else
            {
                solidRadioButton.IsChecked = false;                
                gradientRadioButton.IsChecked = true;                                
                label1.IsEnabled = true;
                gradientPreview.IsEnabled = true;
            }

            solidColor.Background = new SolidColorBrush(conf.background.solidOurColor.GetColor());

            gradientFrom.Background = new SolidColorBrush(conf.background.colorOne.GetColor());
            gradientTo.Background = new SolidColorBrush(conf.background.colorTwo.GetColor());

            LinearGradientBrush lgb = new LinearGradientBrush();
            lgb.StartPoint = new Point(0, 0);
            lgb.EndPoint = new Point(0, 1);
            lgb.GradientStops.Add(new GradientStop(conf.background.colorOne.GetColor(), 1.0));
            lgb.GradientStops.Add(new GradientStop(conf.background.colorTwo.GetColor(), 0.0));
            gradientPreview.Background = lgb;
        }

        private void solidColor_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (solidRadioButton.IsChecked == true)
            {
                System.Windows.Forms.ColorDialog cd = new System.Windows.Forms.ColorDialog();
                System.Windows.Forms.DialogResult res = cd.ShowDialog();
                if (res == System.Windows.Forms.DialogResult.OK)
                {
                    solidColor.Background = new SolidColorBrush(Color.FromRgb(cd.Color.R, cd.Color.G, cd.Color.B));
                }
            }
        }

        public void updatePreview()
        {
            LinearGradientBrush lgb = new LinearGradientBrush();
            lgb.StartPoint = new Point(0, 0);
            lgb.EndPoint = new Point(0, 1);
            lgb.GradientStops.Add(new GradientStop(((SolidColorBrush)gradientFrom.Background).Color, 1.0));
            lgb.GradientStops.Add(new GradientStop(((SolidColorBrush)gradientTo.Background).Color, 0.0));
            gradientPreview.Background = lgb;
        }

        private void gradientFrom_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (gradientRadioButton.IsChecked == true)
            {
                System.Windows.Forms.ColorDialog cd = new System.Windows.Forms.ColorDialog();
                System.Windows.Forms.DialogResult res = cd.ShowDialog();
                if (res == System.Windows.Forms.DialogResult.OK)
                {
                    gradientFrom.Background = new SolidColorBrush(Color.FromRgb(cd.Color.R, cd.Color.G, cd.Color.B));
                    updatePreview();
                }
            }
        }

        private void gradientTo_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (gradientRadioButton.IsChecked == true)
            {
                System.Windows.Forms.ColorDialog cd = new System.Windows.Forms.ColorDialog();
                System.Windows.Forms.DialogResult res = cd.ShowDialog();
                if (res == System.Windows.Forms.DialogResult.OK)
                {
                    gradientTo.Background = new SolidColorBrush(Color.FromRgb(cd.Color.R, cd.Color.G, cd.Color.B));
                    updatePreview();
                }
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void solidRadioButton_Checked(object sender, RoutedEventArgs e)
        {
            if ((sender as RadioButton).IsChecked == true)
            {
                if (label1 != null)
                {
                    label1.IsEnabled = false;                    
                    gradientPreview.IsEnabled = false;
                }
            }
        }

        private void gradientRadioButton_Checked(object sender, RoutedEventArgs e)
        {
            if ((sender as RadioButton).IsChecked == true)
            {
                if (label1 != null)
                {
                    label1.IsEnabled = true;                    
                    gradientPreview.IsEnabled = true;
                }
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            if (solidRadioButton.IsChecked == true)
            {
                conf.background.backgroundType = TouchUMLConfiguration.Background.BackgroundType.SOLID;
                conf.background.solidOurColor = new TouchUMLConfiguration.OurColor(((SolidColorBrush)solidColor.Background).Color);
            }
            else
            {
                conf.background.backgroundType = TouchUMLConfiguration.Background.BackgroundType.GRADIENT;
                conf.background.colorOne = new TouchUMLConfiguration.OurColor(((SolidColorBrush)gradientFrom.Background).Color);
                conf.background.colorTwo = new TouchUMLConfiguration.OurColor(((SolidColorBrush)gradientTo.Background).Color);                
            }

            conf.ApplicationTheme = (TouchUMLConfiguration.AppTheme)presetComboBox.SelectedIndex;

            Window1.configuration = conf;

            this.Close();
        }

        private void presetComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int id = ((ComboBox)sender).SelectedIndex;
            gradientRadioButton.IsChecked = true;
            solidRadioButton.IsChecked = false;

            if (id == 0)
            {
                gradientFrom.Background = new SolidColorBrush(Color.FromRgb(255, 255, 255));
                gradientTo.Background = new SolidColorBrush(Color.FromRgb(191, 219, 255));                
            }
            else if (id == 1)
            {
                gradientFrom.Background = new SolidColorBrush(Color.FromRgb(255, 255, 255));
                gradientTo.Background = new SolidColorBrush(Color.FromRgb(220, 220, 220));                
            }
            else if (id == 2)
            {
                gradientFrom.Background = new SolidColorBrush(Color.FromRgb(70, 70, 70));
                gradientTo.Background = new SolidColorBrush(Color.FromRgb(50, 50, 50));
            }

            updatePreview();
        }
    }
}
